define(function (require, exports, module) {
    require("apps/dataimport/page.css");
    var cop = require("apps/data-import");
    require("apps/grid");

    exports.init = function (ctx, uuid, config) {
        var page = new $.PageDomFactory(uuid);
        page.getJqDom("downPre").linkbutton();
        page.getJqDom("showDes").linkbutton({
            onClick: function () {
                var panel = page.getJqDom("des");
                panel.is(":visible") ? panel.hide() : panel.show();
            }
        });

        require("apps/file");
        page.getJqDom("fileSelect").fileupload({
            width:120,
            selectText:"选择导入文件",
            fileTypeDesc: '*.xls;*.xlsx;',
            fileTypeExts: '*.xls;*.xlsx;',
            uploader: ctx + "/app/api/file/uploadTempFile",
            onUploadSuccess: function (files) {
                var file = files[0].href, fileName = files[0].name;
                page.getJqDom("fileSelect").val(file);
                page.getJqDom("filePath").html(fileName);
                page.getJqDom("action").show();
            }
        });

        page.getJqDom("doUpload").linkbutton({
            onClick: function () {
                var file = page.getJqDom("fileSelect").val();
                var fileName = page.getJqDom("filePath").html();
                cop.doUpload({
                    keyCode: config.keyCode,
                    filePath: file,
                    fileName: fileName,
                    postData: config.postData,
                    processPanel:page.getJqDom("progress"),
                    onWhenStart:function(){
                        page.getJqDom("workJob").hide();
                    },
                    onWhenEnd:function(){
                        page.getJqDom("workJob").show();
                        pageListComponent.grid.datagrid("reload");
                    }
                })
            }
        });

        var pageListComponent = new $.PageListComponent(uuid, {
            view: {
                grid: "grid",
                colbar: "user_list_btn"
            },
            configs: {
                gridTitle: "模板列表",
                gridUrl: ctx + "/app/api/dataImport/histories?keyCode=" + config.keyCode,
                gridColumns: [
                    {
                        field: 'id',
                        checkbox: true
                    },
                    {
                        field: 'temp1',
                        hidden: true
                    },
                    {
                        field: 'importUser.userName',
                        title: '导入人',
                        width: 100,
                        align: 'left'
                    },
                    {
                        field: 'importTime',
                        title: '导入时间',
                        width: 100,
                        align: 'left'
                    },
                    {
                        field: 'totalNum',
                        title: '总数据',
                        width: 50,
                        align: 'left'
                    },
                    {
                        field: 'result',
                        title: '导入结果',
                        width: 100,
                        align: 'left',
                        formatter: function (value, row, index) {
                            if (value == "RESULT_SUCCESS") {
                                return "成功";
                            } else {
                                return "失败，失败数" + row.failureNum;
                            }
                        }
                    }
                ]
            },
            events: {
                renderColBarBefore: function (key, row) {
                    if(key == "download"){
                        return row.result == "RESULT_FAILURE";
                    }
                    return true;
                },
                onClickToolbarBtn: function (context, btn) {
                    switch (btn.key) {
                        default :
                            break;
                    }
                }
            }
        });
    }
});